package dataBase;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;

public class SqlTable implements ISqlTable {
	String tableName = "";
	List<ColumnValue> schema = null;
	String engine = "";
	final static String INNO = "INNODB";
	final static String ISAM = "MYISAM";
	
	public SqlTable(String tableName,List<ColumnValue> schema, String engine) {
		if ((tableName.equals(""))
				|| (schema == null)
				|| ((!engine.toUpperCase().equals(INNO)) && (!engine
						.toUpperCase().equals(ISAM)))) {
			throw new IllegalArgumentException();
		}
		this.tableName = tableName;
		this.schema = schema;
		this.engine = engine;
	}

	@Override
	public List<ColumnValue> getSchema() {
		return schema;
	}

	@Override
	public boolean createTable(Statement stmt) throws SQLException {
		if (stmt == null){
			return false;
		}
		String createQuery = "CREATE TABLE " + tableName;
		createQuery += "(" + createColumns(schema) + ") engine = " + engine
				+ ";";
		boolean res;
		try{
			res =  stmt.execute(createQuery);
		}
		catch (MySQLSyntaxErrorException e){
			// table already exist
			return true;
		}
		return res;
	}

	@Override
	public boolean createTable(Statement stmt, List<String> primaryKey) throws SQLException {
		if (stmt == null){
			return false;
		}
		String createQuery = "CREATE TABLE " + tableName;
		createQuery += "(" + createColumns(schema) + ", primary key(";
		for (String key : primaryKey) {
			createQuery += key + ",";
		}
		createQuery = createQuery.substring(0,createQuery.length() - 1);
		createQuery += ")) engine = " + engine + ";";//TODO: update function
		boolean res;
		try {
			res = stmt.execute(createQuery);
		}
		catch (SQLException e){
			String query = "SELECT * FROM " + tableName + ";";
			try {
				res = stmt.execute(query);
			}
			catch(SQLException ex){
				return false;
			}
			// table already exists
			return true;
		}
		return res;
	}

	@Override
	public boolean insertRecord(Statement stmt, List<String> values)
			throws SQLException {
		return insertRecord(stmt, values, false);
	}
	
	@Override
	public boolean insertRecord(Statement stmt, List<String> values, boolean ignore) throws SQLException {
		if ((stmt == null) || (values == null)){
			return false;
		}
		String toIgnore = "";
		if (ignore){
			toIgnore = " ignore";
		}
		String insertQuery = "insert" + toIgnore+ " into " + tableName + " values(";
		for (String value : values) {
			insertQuery += "\"" + value + "\"" + "," ;		
		}
		insertQuery = insertQuery.substring(0,insertQuery.length()-1) + ");";
		return stmt.execute(insertQuery);
	}

	String createColumns(List<ColumnValue> schemas) {
		String res = "";
		for (ColumnValue column : schemas) {
			res += column.value + " " + column.type + ",";
		}
		res = res.substring(0, res.length() - 1);
		return res;
	}

	@Override
	public boolean createTable(Statement stmt, List<String> primaryKey, List<String> fullText) throws SQLException {
		if ((stmt == null) || (fullText == null)){
			return false;
		}
		String createQuery = "CREATE TABLE " + tableName;
		createQuery += "(" + createColumns(schema);
		if (primaryKey != null)
		{
			createQuery += ", primary key(";
			for (String key : primaryKey) {
				createQuery += key + ",";
			}
			createQuery = createQuery.substring(0,createQuery.length() - 1) + ")";
		}
		if (fullText != null)
		{
			createQuery += ", FULLTEXT(";
			for (String key : fullText) {
				createQuery += key + ",";
			}
			createQuery = createQuery.substring(0,createQuery.length() - 1) + ")";
		}
		createQuery += ") engine = " + engine + ";";//TODO: update function
		boolean res;
		try {
			res = stmt.execute(createQuery);
		}
		catch (SQLException e){
			String query = "SELECT * FROM " + tableName + ";";
			try {
				res = stmt.execute(query);
			}
			catch(SQLException ex){
				return false;
			}
			// table already exist
			return true;
		}
		return res;
	}

	public String getName() {
		return tableName;
	}

}
